package cn.bytengine.stem.module;

import cn.bytengine.stem.contexts.Context;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public abstract class SpringModuleFactory<M extends Module> implements ApplicationContextAware, ModuleFactory<M> {
    private ApplicationContext applicationContext;
    private Context config;
    private String moduleName;

    protected SpringModuleFactory(String moduleName, Context config) {
        this.config = config;
        this.moduleName = moduleName;
    }

    protected abstract Class<M> getModuleClass();

    public M createModule() {
        return applicationContext.getBean(getModuleClass());
    }

    @Override
    public Context getConfig() {
        return config;
    }

    @Override
    public String getModuleName() {
        return moduleName;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
